package m.jclecai.com.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.BuyDetailActivity;
import m.jclecai.com.bean.AccountParticularsBean;
import m.jclecai.com.bean.GroupBuyBean;
import m.jclecai.com.contents.LotteryType;

/**
 * Created by ZD on 2017/6/27.
 */

public class RedPkgDetailHolder extends ViewHolder {
    private final Context mContext;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_balance)
    TextView tvBalance;

    public RedPkgDetailHolder(Context context, @LayoutRes int resource) {
        super(context, resource);
        mContext = context;
    }

    public void init(final AccountParticularsBean bean) {
        tvDesc.setText(bean.getOrderName());
        tvTime.setText(formatTime(bean.getAddtime()));
        tvTitle.setVisibility(View.VISIBLE);

        Drawable redpack = mContext.getResources().getDrawable(R.drawable.redpack);

        //撤单返款
        if ("0".equals(bean.getItype())) {
            tvMoney.setText("+" + bean.getBack_money());
            tvMoney.setTextColor(ContextCompat.getColor(getContext(), R.color.font_red));
            tvTitle.setVisibility(View.INVISIBLE);
            redpack=mContext.getResources().getDrawable(R.drawable.redpack2);
        } else {
            tvMoney.setText("-" + bean.getGo_money());
            tvMoney.setTextColor(ContextCompat.getColor(getContext(), R.color.color_40a68d));
        }
        //根据彩种id来设置相应的文字；
        if (bean.getLotteryid().equals("0")) {
            tvTitle.setText("赠送红包");
        }else {
            tvTitle.setText(LotteryType.getLotteryName(bean.getLotteryid()));
        }
        tvBalance.setText("余额:" + bean.getBalance());
        tvMoney.setCompoundDrawablesWithIntrinsicBounds(redpack,null,null,null);

        mView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getLotteryid().equals("0")) {
                    return;
                }
                GroupBuyBean gb = new GroupBuyBean(bean.getPid(), bean.getProjectno());
                mContext.startActivity(new Intent(mContext, BuyDetailActivity.class)
                        .putExtra("bean", JSON.toJSONString(gb)));
            }
        });
    }

    String formatTime(String start) {
        start = start.substring(start.indexOf("-") + 1, start.lastIndexOf(":"));
        return start;
    }
}
